package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import javax.swing.Timer;

import Vue.*;
import Modele.*;

public class CtrlAppli {
    private List<Fenetre> fenetres;
    private static CtrlMonde ctrl_monde;
    private Timer timer;
    
    public CtrlAppli(){
    	this.fenetres = new ArrayList<Fenetre>();
    	
    	ActionListener action = new ActionListener()
        {   
            @Override
            public void actionPerformed(ActionEvent event)
            {
            	CtrlAppli.ctrl_monde.run();
            }
        };
        this.timer = new Timer(500, action);
    	
    }
    
    public void ajoutFenetre(Fenetre _fen){
    	this.fenetres.add(_fen);
    }
    
    public void removeFenetre(Fenetre _fen){
    	this.fenetres.remove(_fen);
    }
    
    public void demarrer()
    {
    	if(ctrl_monde!=null)
    		{   		
    				timer.setInitialDelay(0);
    		        timer.start();
    		}
    }
    
    public void stopper()
    {
    	if(ctrl_monde!=null)
		{   	
    		if (timer != null)
    		{
    			timer.stop();
    		}
	    		  	    			    			  				
		}
    }
    
    public void accelerer()
    {
    	if(ctrl_monde!=null)
		{   	
    		if (timer != null)
    		{
    			timer.setDelay(timer.getDelay()/2);
    		}
	    		  	    			    			  				
		}
    }
    
    public void ralentir()
    {
    	if(ctrl_monde!=null)
		{   	
    		if (timer != null)
    		{
    			timer.setDelay(timer.getDelay()*2);
    		}
	    		  	    			    			  				
		}
    }
    
    public CtrlMonde getCtrl_monde() {
		return ctrl_monde;
	}

	public void setCtrl_monde(CtrlMonde ctrl_monde) {
		this.ctrl_monde = ctrl_monde;
	}

	public void refreshFenetres(){
    	for (Iterator<Fenetre> i = this.fenetres.iterator(); i.hasNext();){
    		i.next().refresh();
    		
    	}
    }
	
	public int sauvegarderCarte(String path)
	{
		try {
			if(this.getCtrl_monde() != null)
			{
				if(this.getCtrl_monde().getMonde()!=null)
				{
			        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));
			        out.writeObject(this.getCtrl_monde().getMonde());
			        out.close();
			        return 0;
				}
			}
		} catch( IOException e ) {
		} 
		return 1;
	}
	
	public int chargerCarte(String path)
	{
		try {
	        ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
	        Monde monMondeCharge = (Monde)in.readObject();
	        in.close();
	        if(monMondeCharge!=null)
	        {
		        if(this.getCtrl_monde()!=null)
		        {
		        	this.getCtrl_monde().setMonde(monMondeCharge);
		        	return 0;
		        }
	        }
	        in.close();
		} catch( ClassNotFoundException e1 ) {
		} catch( IOException e2 ) {
		} 
		return 1;
	}
}
